<template>
  <el-dialog :title="title" :visible="isshowdialog" :destroy-on-close="true" @close="btnEsc">
    <el-form
      ref="Form"
      label-width="120px"
      :model="partmentdetail"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="partmentdetail.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="partmentdetail.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="partmentdetail.manager"
          placeholder="请选择部门负责人"
        >
          <el-option
            v-for="(item, index) in managers"
            :key="index"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="partmentdetail.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click.native="Btnconfirm(partmentdetail)"
        >确定</el-button>
        <el-button size="small" @click="btnEsc">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { get_company_department } from '@/api/department'
export default {
  props: {
    isshowdialog: {
      type: Boolean,
      default: false
    },
    partmentdetail: {
      type: Object,
      required: true
    },
    title: {
      type: String,
      default: '新增部门'
    },
    managers: {
      type: Array,
      required: true
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNodeTrue = async(rules, val, callback) => {
      const { depts } = await get_company_department();
      (rules.field === 'name' ? (this.partmentdetail.id ? depts.filter(item => item.pid === this.partmentdetail.pid).some(value => value[rules.field] === val) : depts.filter(item => item.pid === this.treeNode.id).some(value => value[rules.field] === val)) : depts.some(value => value[rules.field] === val && value.id !== this.treeNode.id)) ? (rules.field === 'name' ? callback(new Error(`同一部门下${rules.field}不能重复`)) : callback(new Error(`${rules.field}必须唯一`))) : callback()
    }
    return {
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 16, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkNodeTrue, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 16, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkNodeTrue, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  methods: {
    Btnconfirm(partmentdetail) {
      this.$refs['Form'].validate((valid) => {
        if (valid) {
          this.$emit('Btnconfirm', partmentdetail)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    btnEsc() {
      this.$refs['Form'].resetFields()
      this.$emit('btnEsc')
    }
  }
}
</script>

<style>
</style>
